package control
{
	import flash.events.Event;
	
	import model.ModelLocator;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.http.HTTPService;
	import mx.rpc.remoting.RemoteObject;
	
	import view.Plans;
	import view.msg;
	import view.routeDetails;
	public class showMyRoutes
	{
		[Bindable]
		public var details:routeDetails;
		internal var theModel:ModelLocator = ModelLocator.getInstance();
		private var showRO:RemoteObject;
		private var deleteRO:RemoteObject;
		private var showMessageHttp:HTTPService;
		
		private var message:msg;
		//private var dbShow:showMyRoutes = new showMyRoutes();
		public function showMyRoutes()
		{
			showRO = new RemoteObject();
			deleteRO =new RemoteObject();
			
			showRO.destination="show";
			deleteRO.destination="delete";
			showRO.showAllPath.addEventListener(ResultEvent.RESULT,allResultHandler);
			
			showRO.showMyPath.addEventListener(ResultEvent.RESULT,resultHandler);
			
			deleteRO.deletePath.addEventListener(ResultEvent.RESULT,deletePathHandler);
			
			
			showRO.showMessenger.addEventListener(ResultEvent.RESULT,showMessageHandler);
			
			showRO.addEventListener(FaultEvent.FAULT,fHandler);
			deleteRO.addEventListener(FaultEvent.FAULT,fHandler);
		}
		public function show(userid:int):void{
			showRO.showMyPath(userid);	
		}
		public function showRoutes(pathid:int):void{
			showRO.showRoutesDetails.addEventListener(ResultEvent.RESULT,routesResultHandler);
			showRO.showRoutesDetails(pathid);
		}
		public function showPopRoutes(pathid:int,i:routeDetails):void{
			details=i;
			showRO.showRoutesDetails.addEventListener(ResultEvent.RESULT,routesPopResultHandler);
			
			showRO.showRoutesDetails(pathid);
		}
		public function showAll():void{
			showRO.showAllPath();
		}
		public function showMessage(pathid:int,m1:msg):void{
			
			this.message=m1;
			showRO.showMessenger(pathid);
		}
		public function deletePath(pathid:int):void{
			deleteRO.deletePath(pathid);
		}
		private function httpEncoding(param:String):String{
                return encodeURIComponent(param);
        }
       	internal function resultHandler(event:ResultEvent):void{
       		
       		Plans.allRoutes=event.result as ArrayCollection;
       		
       	}
		internal function routesResultHandler(event:ResultEvent):void{
				//theModel.allRouteDetails.removeAll();
       			Plans.allRouteDetails=event.result as ArrayCollection;
       			theModel.dispatchEvent(new Event(ModelLocator.SHOW_ROUTES_OK));
   			
       	}
       	internal function routesPopResultHandler(event:ResultEvent):void{
       		details.allRouteDetails=event.result as ArrayCollection;
       	}
       	internal function allResultHandler(event:ResultEvent):void{
       		
			 theModel.allPlans=event.result as ArrayCollection;
       		 //Plans.myShare=theModel.allPlans;
   			
       	}
       	internal function deletePathHandler(event:ResultEvent):void{
       		if(event.result as Boolean){
       			Alert.show("delete ok");
       			showRO.showAllPath();
       		}else{
       			Alert.show("delete fail");
       			showRO.showMyPath(theModel.user.user_id);
       		}
       	}
       	internal function showMessageHandler(event:ResultEvent):void{
       		
       		message.allMessages=event.result as ArrayCollection;
       		
       	}
		internal function fHandler (event:FaultEvent):void {
         		Alert.show(event.fault.message,"Error1")
        }

	}
}